package com.zzl.finance.order.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.finance.order.dto.EmailAddDto;
import com.zzl.finance.order.dto.EmailUpdateDto;
import com.zzl.finance.order.dto.OrderInvoiceAddDto;
import com.zzl.finance.order.dto.OrderInvoiceUpdateDto;
import com.zzl.finance.order.entity.EmailEntity;
import com.zzl.finance.order.entity.OrderInvoiceEntity;
import com.zzl.finance.order.service.EmailService;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @author sunpp
 * @date 2022-03-07 10:41:16
 */
@ApidocController("邮件")
@RestController
@SuppressWarnings("javadoc")
@RequestMapping("order/email")
@Slf4j
public class EmailController extends WebBaseCtrl {
    @SuppressWarnings("all")
    @Autowired
    private EmailService emailService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApidocOperation(description = "分页查询数据", operationId = "/order/orderinvoice/list")
    @ApidocReturnPage(genericTypeClass = EmailEntity.class)
    public Result list(@ApidocParamsPage(name = "params") @RequestParam Map<String, Object> params) {
        PageUtils page = emailService.queryPage(params, this.getUser());
        return Result.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApidocOperation(description = "通过id获取数据", operationId = "/order/orderinvoice/info")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "orderInvoice", valueClass = EmailEntity.class)
    })
    public Result info(@ApidocParam(name = "id", required = true) @PathVariable("id") Integer id) {
        EmailEntity orderInvoice = emailService.getById(id);
        return Result.ok().put("orderInvoice", orderInvoice);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApidocOperation(description = "保存", operationId = "/order/orderinvoice/save")
    @ApidocReturnR
    public Result save(@ApidocParam(name = "保存的对象值", required = true) @RequestBody @Validated EmailAddDto emailAddDto) {
        try {
            emailService.saveEntity(emailAddDto, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApidocOperation(description = "修改", operationId = "/order/orderinvoice/update")
    @ApidocReturnR
    public Result update(@ApidocParam(name = "修改的对象值", required = true) @RequestBody EmailUpdateDto emailUpdateDto) {
        try {
            emailService.updateEntity(emailUpdateDto, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApidocOperation(description = "删除", operationId = "/order/orderinvoice/delete")
    @ApidocReturnR
    public Result delete(@ApidocParam(name = "删除的对象id集合", required = true) @RequestBody Integer[] ids) {
        emailService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

}
